package com.buildings.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.buildings.R;


/***
 * 对话框工具类
 *
 * @author CRay
 */
public class DialogUtils {


    /***
     * 刷新Dialog
     *
     * @param mContext
     * @return
     */
    public static Dialog showRefreshDialog(Context mContext) {
        Dialog dialog = new Dialog(mContext, R.style.monitor_dialog);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_refresh, null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        return dialog;
    }

    /***
     * 房屋类型对话框Dialog
     */
    public static Dialog showBuildSelectDialog(Context mContext,
                                              final OnClickListener clickListener, boolean isTouchClose) {
        final Dialog dialog = new Dialog(mContext, R.style.dialog_anim);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_building_type_select, null);
        dialog.setContentView(view);

        view.findViewById(R.id.tv_town_house).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setTag(1);
                clickListener.onClick(view);
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_town_no_house).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setTag(2);
                clickListener.onClick(view);
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_village_house).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setTag(3);
                clickListener.onClick(view);
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_village_public_build).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setTag(4);
                clickListener.onClick(view);
                dialog.dismiss();
            }
        });

        dialog.setCanceledOnTouchOutside(isTouchClose);
        if (dialog != null)
            dialog.show();
        return dialog;
    }


    /***
     * 仿ios对话框提示 取消，确定
     *
     * @param mContext
     * @return
     */
    public static Dialog showIOSDialog(Context mContext,String title, String btnMessage1,String color1,String btnMessage2,String color2, final OnClickListener cl1,final OnClickListener cl2,String content) {
        final Dialog dialog = new Dialog(mContext, R.style.dialog_anim);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_ios, null);
        RelativeLayout rl_content = view.findViewById(R.id.rl_content);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        if(TextUtils.isEmpty(title)){
            tvTitle.setVisibility(View.GONE);
        }else {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        TextView tvContent = view.findViewById(R.id.tv_content);
        if(TextUtils.isEmpty(content)){
            tvContent.setVisibility(View.GONE);
        }else {
            tvContent.setVisibility(View.VISIBLE);
            tvContent.setText(content);
        }
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rl_content.getLayoutParams();
        lp.width = (int) (0.72*mContext.getResources().getDisplayMetrics().widthPixels);
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        rl_content.setLayoutParams(lp);
        dialog.setContentView(view);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        if(!TextUtils.isEmpty(btnMessage1)){
            tvCancel.setText(btnMessage1);
        }
        if(!TextUtils.isEmpty(color1)){
            tvCancel.setTextColor(Color.parseColor(color1));
        }
        tvCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if(cl1 != null)
                    cl1.onClick(arg0);
                dialog.dismiss();
            }
        });
        TextView tvCommit = view.findViewById(R.id.tv_commit);
        if(!TextUtils.isEmpty(btnMessage2)){
            tvCommit.setText(btnMessage2);
        }
        if(!TextUtils.isEmpty(color2)){
            tvCommit.setTextColor(Color.parseColor(color2));
        }
        tvCommit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if(cl2 != null)
                    cl2.onClick(arg0);
                dialog.dismiss();
            }
        });
        dialog.setCanceledOnTouchOutside(true);
        if (dialog != null)
            dialog.show();
        return dialog;
    }

}
